package com.wanho.java157.controller;

import com.wanho.java157.Car;
import com.wanho.java157.result.R;
import com.wanho.java158.CarService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/api/core/car")
public class CarController {
    @Resource
    private CarService carService;

    @GetMapping("/index")
    public R listAll(){
        List<Car> carList = carService.selectAll();
        return R.ok().data("list", carList).message("获取列表成功");
    }

    @DeleteMapping("/remove/{carId}")
    public R removeById(
            @PathVariable Integer carId){
        boolean result = carService.deleteByPrimaryKey(carId)>0;
        if(result){
            return R.ok().message("删除成功");
        }else{
            return R.error().message("删除失败");
        }
    }

    @PostMapping("/save")
    public R save(
            @RequestBody Car record){
        boolean result = carService.insert(record) >0 ;
        if(result){
            return R.ok().message("保存成功");
        }else{
            return R.error().message("保存失败");
        }
    }
}
